package cn.xuyun.cc.tcp.vo;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;

public class Entity {

	private long magic; // PKGMGR , 8 bytes

	private int version;// 版本号,目前为1

	private int data_len;// 数据长度

	private int send_time;// 发送时间

	private int serial;// 数据序列号

	private int type;// 类型，0请求，1回复

	private int cmd;// 命令编码

	private int content_len;// 内容长度

	private byte[] content;// 内容

	public Entity() {
		// TODO Auto-generated constructor stub
	}

	public Entity(int time, int version, int serial, int type, int cmd, byte[] content) {
		this.cmd = cmd;
		this.send_time = time;
		this.version = version;
		this.serial = serial;
		this.type = type;
		this.content = content;
		this.magic = Constants.magic;
		this.content_len = content.length;
		this.data_len = 20 + content.length;
	}

	public long getMagic() {
		return magic;
	}

	public void setMagic(long magic) {
		this.magic = magic;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public int getData_len() {
		return data_len;
	}

	public void setData_len(int data_len) {
		this.data_len = data_len;
	}

	public int getSend_time() {
		return send_time;
	}

	public void setSend_time(int send_time) {
		this.send_time = send_time;
	}

	public int getSerial() {
		return serial;
	}

	public void setSerial(int serial) {
		this.serial = serial;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getCmd() {
		return cmd;
	}

	public void setCmd(int cmd) {
		this.cmd = cmd;
	}

	public int getContent_len() {
		return content_len;
	}

	public void setContent_len(int content_len) {
		this.content_len = content_len;
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	public byte[] getBytes() {
		ChannelBuffer totalBuffer = ChannelBuffers.dynamicBuffer();

		totalBuffer.writeLong(this.getMagic());// 魔术字
		totalBuffer.writeInt(this.getVersion());
		totalBuffer.writeInt(this.getData_len());
		totalBuffer.writeInt(this.getSend_time());
		totalBuffer.writeInt(this.getSerial());
		totalBuffer.writeInt(this.getType());
		totalBuffer.writeInt(this.getCmd());
		totalBuffer.writeInt(this.getContent_len());
		totalBuffer.writeBytes(this.getContent());

		byte[] b = new byte[totalBuffer.readableBytes()];

		totalBuffer.readBytes(b);

		return b;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		sb.append("hex:" + HexBin.encode(this.getBytes()) + "\r\n");
		sb.append("magic:" + this.getMagic() + "\r\n");
		sb.append("version:" + this.getVersion() + "\r\n");
		sb.append("data_len:" + this.getData_len() + "\r\n");
		sb.append("send_time:" + this.getSend_time() + "\r\n");
		sb.append("serial:" + this.getSerial() + "\r\n");
		sb.append("type:" + this.getType() + "\r\n");
		sb.append("cmd:" + this.getCmd() + "\r\n");
		sb.append("content_len:" + this.getContent_len() + "\r\n");
		sb.append("content:" + new String(this.getContent()) + "\r\n");
		return sb.toString();
	}

}
